package com.lijin.dao;

import com.lijin.dao.provider.LotteryInformationProvider;
import com.lijin.domain.entity.LotteryInformation;
import com.lijin.domain.query.LotteryInformationQuery;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

/**
 * Created by gold-jx on 2017/5/27.
 */
public interface LotteryInformationDao {

  String BASE_RESULT = "lottery_information_result";

  String TABLE_NAME = "lottery_information";

  String COLUMNS = "lottery_code, lottery_expect, open_code, open_time, create_time, state, version, source, orig_time";

  /**
   * 增加
   */
  @Insert("INSERT INTO " + TABLE_NAME + " (" + COLUMNS + ") VALUES (" +
      "#{lotteryCode},#{lotteryExpect},#{openCode},#{openTime},now(),#{state} ,1 ,#{source},now())")
  public int save(LotteryInformation lotteryInformation);


  /**
   * 修改
   */
  @UpdateProvider(type = LotteryInformationProvider.class, method = "updateById")
  public int update(LotteryInformation lotteryInformation);


  /**
   * 删除
   */
  @Update("UPDATE " + TABLE_NAME + " SET status='DELETED' WHERE id = #{id}")
  public int delete(String id);


  /**
   * 查询
   */
  @SelectProvider(type = LotteryInformationProvider.class, method = "findListByQuery")
  List<LotteryInformation> findListByQuery(LotteryInformationQuery query);

  /**
   * 查询数
   * @param query
   * @return
   */
  @SelectProvider(type = LotteryInformationProvider.class, method = "findCountByQuery")
  int findCountByQuery(LotteryInformationQuery query);

  @Select("select * from lottery_information where lottery_code=#{lotteryCode} and lottery_expect=#{lotteryExpect} and open_code=#{opencode} and state='NORMAL' limit 1 ")
  @ResultMap(BASE_RESULT)
  LotteryInformation findByCodeAndExpect(@Param("lotteryCode") String lotteryCode,
          @Param("lotteryExpect") String lotteryExpect, @Param("opencode") String opencode);

  /**
   * 根据类型查询的开奖信息
   */
  @Select("select lottery_expect,lottery_code,open_code,open_time,state,version from lottery_information where version>0 and lottery_code=#{code} and state='NORMAL' order by open_time desc limit #{pageSize}")
  @Results(
      id = BASE_RESULT,
      value = {
          @Result(column = "lottery_code", property = "lotteryCode"),
          @Result(column = "lottery_expect", property = "lotteryExpect"),
          @Result(column = "open_code", property = "openCode"),
          @Result(column = "open_time", property = "openTime"),
          @Result(column = "create_time", property = "createTime")
      })
  List<LotteryInformation> findSizeInfo(@Param("code") String code,
          @Param("pageSize") int pageSize);

  /**
   * 查找所有最新的开奖信息
   */
  @Select(
      "select A.lottery_expect AS lottery_expect,A.lottery_code AS lottery_code,A.version,A.open_code AS open_code,A.open_time AS open_time,A.create_time AS create_time from lottery_information as A, "
          +
          "(SELECT MAX(lottery_expect) AS lottery_expect,lottery_code FROM lottery_information GROUP BY lottery_code ) AS B "
          +
          "where A.version>0 and A.lottery_expect=B.lottery_expect AND A.lottery_code=B.lottery_code order by A.lottery_code")
  @ResultMap(BASE_RESULT)
  List<LotteryInformation> findNewInfo();

  /**
   * 查找所有最新的开奖信息
   */
  @Select("select * from lottery_information where version>0 GROUP BY lottery_code  ORDER BY open_time desc limit #{pageSize}")
  @ResultMap(BASE_RESULT)
  List<LotteryInformation> findInfo(@Param("pageSize") int pageSize);


  @Update("UPDATE lottery_information SET version=version+1 ,orig_time=now() where id=#{id} ")
  public void addVersion(@Param("id") Long id);

  @Select("select * from lottery_information where version>0 and lottery_code=#{code} and lottery_expect=#{expect}")
  @ResultMap(BASE_RESULT)
  LotteryInformation findInformationByCodeAndExpect(@Param("code") String code,
          @Param("expect") String expect);

  @InsertProvider(type = LotteryInformationProvider.class, method = "insertAll")
  public int insertAll(@Param("list") List<LotteryInformation> list);

}

